package controllers;

import java.util.Calendar;
import java.util.List;

import models.PunchTime;
import models.TimeUser;
import play.data.validation.Valid;
import play.mvc.Controller;
import play.mvc.With;

import com.google.gson.JsonObject;

import errors.EntityNotFound;

@With(CatchError.class)
public class ClockManager extends Controller {

	public static void clock(@Valid PunchTime time) throws EntityNotFound {
		if (!validation.hasErrors()) {
			time.timeStamp = Calendar.getInstance().getTime();
			time.user = TimeUser.findByTag(time.user.tagId);
			time.save();
			JsonObject res = new JsonObject();
			res.addProperty("error", false);
			renderJSON(res.toString());

		} else {
			CatchError.sendErrors();
		}
	}

	public static void timepoints(String tagId) throws EntityNotFound {
		TimeUser user = TimeUser.findByTag(tagId);
		List<PunchTime> points = user.findPointsForDay(Calendar.getInstance()
				.getTime());
		renderJSON(points);
	}
}
